package com.gly091020.mixin;

import com.gly091020.MathDragon;
import com.gly091020.MathDragonFun;
import com.gly091020.client.screen.MathScreen;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.decoration.EndCrystalEntity;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(EndCrystalEntity.class)
public class EndCrystalEntityMixin {
    @Redirect(method = "damage", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/decoration/EndCrystalEntity;isInvulnerableTo(Lnet/minecraft/entity/damage/DamageSource;)Z"))
    public boolean playerAttack(EndCrystalEntity instance, DamageSource damageSource){
        if (!MathDragon.CONFIG.useMathDragon || !MathDragon.CONFIG.switchFunction.endCrystal){
            return instance.isInvulnerableTo(damageSource);
        }
        if (instance.shouldShowBottom() && damageSource.getAttacker() instanceof PlayerEntity){
            if (instance.getWorld().isClient) {
                MathScreen.open(MathDragonFun.getRandomMathematical(), instance.getId());
            }
            return true;
        }
        return instance.isInvulnerableTo(damageSource);
    }


}
